﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MakeTableTool.Template;
using OfficeOpenXml;
namespace MakeTableTool
{
    public class ExcelTable
    {
        public string path;
        public string className;
        public string fileName;
        public List<TableField> tableFields =new List<TableField>();
        public List<TableRow> rowDatas = new List<TableRow>();

        public ExcelTable(string path,ExcelWorksheet worksheet)
        {
            this.path = path;
            fileName = Path.GetFileNameWithoutExtension(path);
            className = "CSV" + TemplateGenTool.CapitalizeFirstLetter(fileName);
            var columnsCnt = worksheet.Dimension.Columns;
            var rowCnt = worksheet.Dimension.Rows;
            

            for (int col = 2; col <= columnsCnt; col++)
            {
                try
                {
                    var define = worksheet.Cells[1, col].Value;
                    var des = worksheet.Cells[2, col].Value;
                    var typeBase = worksheet.Cells[3, col].Value;

                    if (typeBase == null)
                        continue;

                    var name = worksheet.Cells[4, col].Value;
                    var tableField = new TableField();
                    if (define == null)
                        define = "Null";
                    tableField.SetDefine(define.ToString());
                    tableField.SetFieldType(typeBase.ToString());
                    if(des != null)
                        tableField.Description = des.ToString();
                    if(name != null)
                        tableField.Name = name.ToString();

                    tableFields.Add(tableField);
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("文件:{0} 第{1}列 :Message:{2},Stack: ", fileName, col, e.Message, e.StackTrace));
                    
                }
                
            }

            columnsCnt = tableFields.Count; //真正的列数

            //从第三行开始读 (第一行位Define,第二行注释,第三行为数据类型)
            for (int row = 5; row <= rowCnt; row++)
            {
                try
                {
                    TableRow rowData = new TableRow(this.tableFields);
                    for (int col = 2; col <= columnsCnt + 1; col++)
                    {
                        ExcelRange cell = worksheet.Cells[row, col];
                        var index = rowData.dataValue.Count;
                        var define = tableFields[index].define;
                        if (define == TableField.TableDefine.Key)
                        {
                            if (cell.Value == null)
                            {
                                continue;
                            }
                          
                        }

                        rowData.AddData(cell.Value);
                    }

                    var key = rowData.Key;
                    if( rowDatas.Find(x=>x.Key == key) != null)
                    {
                        throw new Exception(string.Format("文件:{0} 含有重复Key {1} ", fileName, key));
                    }
                    rowDatas.Add(rowData);
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("文件:{0} 第{1}行 出错 :{2} ", fileName, row, e.Message));
                }
                
            }
        }


        public string LogData()
        {
            string logStr = "Table : \n";
            foreach (var r in rowDatas)
            {
                logStr += r.LogData();
            }

            return logStr;
        }
    }
}
